<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class Banner extends AppModel {

	public function findByCategoryId($id=null){
		return $this->find('all', array(
			'conditions' => array('category_id'=>$id,'deleted'=>0)
			));
	}
	public function findNameAndId(){
		return $this->query("SELECT a.id, a.img, a.url, b.category_name 
							 from banners as a
							 INNER JOIN categories as b
							 ON b.category_id = a.category_id");
	}
	public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {    
        $recursive = -1;
        // Mandatory to have
        $this->useTable = false;
        $sql = '';
        $sql .= "SELECT banners.id, banners.img, banners.url, categories.category_name 
							 from banners
							 INNER JOIN categories
							 ON banners.category_id = categories.category_id
							 where banners.deleted = 0 and categories.deleted = 0 LIMIT ";
        // Adding LIMIT Clause
        $sql .= (($page - 1) * $limit) . ', ' . $limit;
        $results = $this->query($sql);
        return $results;
    }
	public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $sql = '';
        $sql .= "SELECT a.id, a.img, a.url, b.category_name 
							 from banners as a
							 INNER JOIN categories as b
							 ON b.category_id = a.category_id
							 where a.deleted = 0 and b.deleted = 0
							 ";
        $this->recursive = $recursive;
        $results = $this->query($sql);
        return count($results);
	 }
}
?>
